package cn.wolfcdoe.business.service.impl;

import java.util.List;

import cn.wolfcdoe.business.domain.BusServiceItem;
import cn.wolfcdoe.common.constant.BusServiceItemConstants;
import cn.wolfcdoe.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.stereotype.Service;
import cn.wolfcdoe.business.mapper.BusAppointmentMapper;
import cn.wolfcdoe.business.domain.BusAppointment;
import cn.wolfcdoe.business.service.IBusAppointmentService;
import org.springframework.util.Assert;

/**
 * 养修信息预约Service业务层处理
 * 
 * @author zhuo
 * @date 2023-04-11
 */
@Service
public class BusAppointmentServiceImpl implements IBusAppointmentService 
{
    @Autowired
    private BusAppointmentMapper busAppointmentMapper;

    /**
     * 查询养修信息预约
     * 
     * @param id 养修信息预约主键
     * @return 养修信息预约
     */
    @Override
    public BusAppointment selectBusAppointmentById(Long id)
    {
        return busAppointmentMapper.selectBusAppointmentById(id);
    }

    /**
     * 查询养修信息预约列表
     * 
     * @param busAppointment 养修信息预约
     * @return 养修信息预约
     */
    @Override
    public List<BusAppointment> selectBusAppointmentList(BusAppointment busAppointment)
    {
        return busAppointmentMapper.selectBusAppointmentList(busAppointment);
    }

    /**
     * 新增养修信息预约
     * 
     * @param busAppointment 养修信息预约
     * @return 结果
     */
    @Override
    public int insertBusAppointment(BusAppointment busAppointment)
    {
        //业务判断
        Assert.notNull(busAppointment,"非法参数");
        busAppointment.setCreateTime(DateUtils.getNowDate());
        //设置预约状态
        busAppointment.setStatus(BusServiceItemConstants.APPOINTMENT_STATUS_PENDING);
        return busAppointmentMapper.insertBusAppointment(busAppointment);
    }

    /**
     * 修改养修信息预约
     * 
     * @param busAppointment 养修信息预约
     * @return 结果
     */
    @Override
    public int updateBusAppointment(BusAppointment busAppointment)
    {
        //业务判断
        Assert.notNull(busAppointment,"数据不能为空");
        Long id = busAppointment.getId();
        Assert.notNull(id,"非法参数");
        BusAppointment originBusAppointment = busAppointmentMapper.selectBusAppointmentById(id);
        Assert.notNull(originBusAppointment,"修改的预约信息不存在");
        int status = originBusAppointment.getStatus();
        Assert.state(status == BusServiceItemConstants.APPOINTMENT_STATUS_PENDING
                || status == BusServiceItemConstants.APPOINTMENT_STATUS_ARRIVED,"预约状态不支持修改");
        return busAppointmentMapper.updateBusAppointment(busAppointment);
    }

    /**
     * 批量删除养修信息预约
     * 
     * @param ids 需要删除的养修信息预约主键
     * @return 结果
     */
    @Override
    public int deleteBusAppointmentByIds(Long[] ids)
    {
        return busAppointmentMapper.deleteBusAppointmentByIds(ids);
    }

    /**
     * 删除养修信息预约信息
     * 
     * @param id 养修信息预约主键
     * @return 结果
     */
    @Override
    public int deleteBusAppointmentById(Long id)
    {
        return busAppointmentMapper.deleteBusAppointmentById(id);
    }

    @Override
    public int cancelBusAppointmentById(Long id) {
        Assert.notNull(id,"非法参数");
        BusAppointment originBusAppointment = busAppointmentMapper.selectBusAppointmentById(id);
        Assert.notNull(originBusAppointment,"非法参数");
        int status = originBusAppointment.getStatus();
        Assert.state(status == BusServiceItemConstants.APPOINTMENT_STATUS_PENDING
                || status == BusServiceItemConstants.APPOINTMENT_STATUS_ARRIVED,"预约状态不支持取消");
        originBusAppointment.setStatus(BusServiceItemConstants.APPOINTMENT_STATUS_USER_CANCELED);   //设置用户取消
        return busAppointmentMapper.updateBusAppointment(originBusAppointment);
    }

    @Override
    public int arriveBusAppointmentById(Long id) {
        Assert.notNull(id,"非法参数");
        BusAppointment originBusAppointment = busAppointmentMapper.selectBusAppointmentById(id);
        Assert.notNull(originBusAppointment,"非法参数");
        int status = originBusAppointment.getStatus();
        Assert.state(status == BusServiceItemConstants.APPOINTMENT_STATUS_PENDING,"预约状态不支持取消");
        originBusAppointment.setStatus(BusServiceItemConstants.APPOINTMENT_STATUS_ARRIVED);         //设置用户已到店
        originBusAppointment.setActualArrivalTime(DateUtils.getNowDate());
        return busAppointmentMapper.updateBusAppointment(originBusAppointment);
    }

    @Override
    public int settleBusAppointmentById(Long id) {
        Assert.notNull(id,"非法参数");
        BusAppointment originBusAppointment = busAppointmentMapper.selectBusAppointmentById(id);
        Assert.notNull(originBusAppointment,"非法参数");
        int status = originBusAppointment.getStatus();
        Assert.state(status == BusServiceItemConstants.APPOINTMENT_STATUS_PAYED
                                        || status==BusServiceItemConstants.APPOINTMENT_STATUS_SETTLED
                                        || status==BusServiceItemConstants.APPOINTMENT_STATUS_ARRIVED,"预约状态不支持取消");
        if (status == BusServiceItemConstants.APPOINTMENT_STATUS_ARRIVED) {
            originBusAppointment.setStatus(BusServiceItemConstants.APPOINTMENT_STATUS_SETTLED);
        }
        //设置用户已结算
        return busAppointmentMapper.updateBusAppointment(originBusAppointment);
    }
}
